<template>
	<view class="usersetup">
		<!-- #ifdef MP-WEIXIN -->
		<button open-type="chooseAvatar" @chooseavatar="chooseavatar" class="ys-li head">
			<text>头像</text>
			<view class="name">
				<image :src="avatarUrl || '/static/0.png'" class="avatar" mode="" @click.stop="preview"></image>
				<uni-icons type="right"></uni-icons>
			</view>
		</button>
		<!-- #endif -->

		<!-- #ifndef MP-WEIXIN -->
		<view class="ys-li head" @click="changeImg">
			<text>头像</text>
			<view class="name">
				<image :src="avatarUrl || '/static/0.png'" mode="" class="avatar" @click.stop="preview"></image>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
		<!-- #endif -->

		<navigator url="./setname">
			<view class="ys-li">
				<text>昵称</text>
				<view class="name">
					{{info.nickname || ''}}
					<uni-icons type="right"></uni-icons>
				</view>

			</view>
		</navigator>
	</view>
</template>

<script>
	import {
		updateBase,
		loginUser
	} from "@/api/api.js"
	export default {
		data() {
			return {
				info: [],
				avatarUrl: ''
			}
		},
		onLoad() {
			this.loginUser()
		},

		methods: {
			loginUser() {
				loginUser().then(res => {
					if (res.code == 200) {
						this.avatarUrl = res.data.avatar
						this.info = res.data
					}
				})
			},
			preview() {
				uni.previewImage({
					urls: [this.avatarUrl]
				})
			},
			chooseavatar(e) {
				var that = this
				uni.uploadFile({
					url: `${that.$config.websiteUrl}common/upload`,
					header: {
						'Authorization': uni.getStorageSync('token')
					},
					filePath: e.detail.avatarUrl,
					name: 'file',
					success: (res) => {
						uni.hideLoading()
						let imgRes = JSON.parse(res.data); //微信和头条支持
						if (imgRes.code == 200) {
							let imgData = imgRes
							that.avatarUrl = imgData.url
							updateBase({
								avatar: that.avatarUrl
							}).then(data => {
								if (data.code == 200) {
									uni.setStorageSync('avatar', that.avatarUrl)
								}
							})
						} else {
							uni.$showMsg(imgRes.msg)
							return
						}
					},
					fail(err) {
						uni.hideLoading()
					}
				})
			},
			changeImg() {
				let that = this
				uni.chooseImage({
					sourceType: ['album', 'camera'], //从相册选择
					count: 1,
					success: (res) => {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						uni.uploadFile({
							url: that.$config.requestUrl +
								'/common/upload',
							header: {
								'tokens': uni.getStorageSync('tokens')
							},
							formData: {
								accept: 'image',
								is_avatar: 1
							},
							filePath: igmFile[0],
							name: 'UploadFile',
							success: (res) => {
								uni.hideLoading()
								let imgRes = JSON.parse(res.data); //微信和头条支持
								if (imgRes.code == 200) {
									let imgData = imgRes
									that.avatarUrl = imgData.url
								} else {
									uni.$showMsg(imgRes.msg)
									return
								}
							},
							fail(err) {
								uni.hideLoading()
							}
						})
					},
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #f2f2f2;
		padding-top: 10px;
	}

	.usersetup {
		width: 96%;
		margin: 0 auto;
		border-radius: 10px;

	}

	button {
		line-height: 0;

		&::after {
			border: 0;
		}
	}

	.ys-li {
		font-size: 14px;
		color: #777;
		display: flex;
		background: #FFFFFF;
		justify-content: space-between;
		margin: 0 auto;
		border-bottom: 1px solid #efefef;
		padding: 40rpx;
		align-items: center;

		&.head {
			padding: 10px 20px;
		}

		image {
			width: 30rpx;
			height: 30rpx;
			margin-left: 5px;
		}

		.avatar {
			width: 40px;
			height: 40px;
			border-radius: 4px;
			position: relative;
			z-index: 99;
			margin-right: 8px;
		}

		.name {
			display: flex;
			align-items: center;

		}
	}
</style>